package com.ambitor.ipro.count;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.ambitor.ipro.actions.BaseAction;
import com.ambitor.ipro.util.Constants;
import com.ambitor.ipro.util.SkyenccUtil;
import com.ambitor.ipro.util.TableName;

public class CountAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private String picId = "";

	private BufferedInputStream inputStream;

	@SuppressWarnings("unchecked")
	public String execute() {
		createCountImage();
//		if (StringUtils.isBlank(picId)) {
//			return SUCCESS;
//		}
//
//		HttpServletRequest request = pd.getRequest();
//		String ip = request.getHeader("X-Real-IP");
//		String referer = request.getHeader("Referer");
//		if (StringUtils.isBlank(referer))
//			return SUCCESS;
//
//		String[] strs = Constants.STR_MUST_IN_REFERER_OF_COUNT_URL.split("\\|");
//		boolean valid = false;
//		for (String str : strs) {
//			if (referer.indexOf(str) >= 0) {
//				valid = true;
//			}
//		}
//		if (valid == false) {
//			return SUCCESS;
//		}
//
//		// 保存到本次的请求到IData中
//		IData pvMap = new DataMap();
//		pvMap.put("USER_ID", picId);
//		pvMap.put("URL", referer);
//		pvMap.put("IP", ip);
//		pvMap.put("VISIT_TIME", SkyenccUtil.formatDate(new Date()));
//
//		// 保存到db或暂存到session
//		IData countMap = new DataMap();
//		Object countObjInSession = pd
//				.getApplicationAttribute(Constants.APPLICATION_COUNT);
//		if (countObjInSession != null) {
//			countMap = (IData) countObjInSession;
//		}
//
//		IDataset pvDataset = new DatasetList();
//		Object pvObject = countMap.get(picId);
//		if (pvObject != null) {
//			pvDataset = (IDataset) pvObject;
//		}
//
//		pvDataset.add(pvMap);
//
//		if (pvDataset.size() == Constants.PV_NUM_WHEN_SAVE_TO_DB) {
//			try {
//				Connection conn = pd.getConn();
//				SuperDao superDao = new SuperDaoImpl(conn);
//				superDao.insert(TableName.TL_S_PV, pvDataset);
//			} catch (SQLException e) {
//				logger.warn(this.getPicId() + "获取connection异常");
//				e.printStackTrace();
//			} catch (Exception e) {
//				logger.warn(this.getPicId() + "保存pv异常");
//				e.printStackTrace();
//			}
//			pvDataset.clear();
//		}
//		countMap.put(picId, pvDataset);
//		pd.setApplicationAttribute(Constants.APPLICATION_COUNT, countMap);

		return SUCCESS;
	}

	public BufferedInputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(BufferedInputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getPicId() {
		return picId;
	}

	public void setPicId(String picId) {
		this.picId = picId;
	}

	private void createCountImage() {
		try {
			String path = pd.getApplication().getRealPath("/")
					+ "/img/taozhushou_stat.png";
			File countImageFile = new File(path);
			FileInputStream input = new FileInputStream(countImageFile);
			BufferedInputStream inputStream = new BufferedInputStream(input);
			this.setInputStream(inputStream);
		} catch (Exception e) {
			logger.warn("生成统计图片的过程中出现错误");
			e.printStackTrace();
		}
	}
}
